global class ApexSchedulerGetLoginDate implements schedulable{
        public Integer checkWeekDay(Date inDate)
        {
            date weekStart = inDate.toStartofWeek();
            Integer weekDay = weekStart.daysBetween(inDate);
            return weekDay;
        }

        global void execute(schedulablecontext sc){
            List<Login_History__c> insertList = new List<Login_History__c>();
            List<String> functionDep = new List<String>();
            functionDep.add('Trade Functional');
            functionDep.add('Medical Functional');
            functionDep.add('Trade&Medical Functional');
            Profile admin = [Select id From Profile Where name =: 'System Administrator' or name =: '系统管理员'];
            Boolean weekday = false;
            
            List<String> workType = new List<String>();
            workType.add('拜访');
            workType.add('本地消费者教育活动');
            workType.add('全国医护人员教育活动');
            workType.add('全国消费者教育活动');
            workType.add('学术沙龙');
            workType.add('科内会');
            workType.add('妈妈班');
 
            List<User> allUser = [Select Id, LastLoginDate From User Where IsActive =: true and ProfileId !=: admin.id and  FunctionDepartment__c not in: functionDep  ];
            Set<User> allUserSet = new Set<User>();
            allUserSet.addAll(allUser);

            List<LoginHistory> AllLogHistory = [Select Id, UserId, LoginTime, LoginType,  Browser, Application,  ApiType
                               FROM LoginHistory where  LoginTime <=: datetime.now().addHours(-1) and LoginTime >=: datetime.now().addHours(7).addDays(-1)];
            Map<id,LoginHistory> AllLogHistoryMap = new Map<id,LoginHistory>();
            for(LoginHistory l : AllLogHistory)
            {
                AllLogHistoryMap.put(l.UserId, l);
            }
           
            List<LoginHistory> LogHistory = [Select Id, UserId, LoginTime, LoginType,  Browser, Application,  ApiType
                               FROM LoginHistory where LoginType=:'Other Apex API' and LoginTime <=: datetime.now().addHours(-1) and LoginTime >=: datetime.now().addHours(-1).addDays(-1)];
            
            Map<Id,LoginHistory> LogMap = new Map<Id,LoginHistory>();
            for(LoginHistory log : LogHistory)
            {
                if(!LogMap.containsKey(log.UserId))
                    LogMap.put(log.UserId, log);
            }
            
            List<CallPlan__c> CPList = [Select Monthly_Call_Plan__r.OwnerId From CallPlan__c Where CallPlanDate__c =: date.today() - 1 and CallWorkingtype__c in: workType];
            List<RoutingPlan__c> RPList = [Select RoutingPlanAndVisit__r.OwnerId From RoutingPlan__c Where Plan_Visit_Date__c =: date.today() - 1];
            map<Id,CallPlan__c> CPMap = new map<Id,CallPlan__c>();
            map<Id,RoutingPlan__c> RPMap = new map<Id,RoutingPlan__c>();
            
            for(CallPlan__c cp:CPList)
            {
                CPMap.put(cp.Monthly_Call_Plan__r.OwnerId, cp);
            }
            for(RoutingPlan__c rp: RPList)
            {
                RPMap.put(rp.RoutingPlanAndVisit__r.OwnerId, rp);
            }
            
            for(User u : allUserSet)
            {
                Boolean SetRecord = false;
                Integer weekdayNum = checkWeekDay(date.today() - 1);
                if((weekdayNum == 6)||(weekdayNum == 0)){
                    weekday = false;
                }
                else
                {
                    weekday = true;
                }
                Login_History__c loginHis = new Login_History__c(ownerid = u.id , WeekDay__c = weekday, Owner__c = u.id);
               if(LogMap.containsKey(u.id))
               {
                   loginHis.IsPDA__c = true;
               }
                if(CPMap.containsKey(u.id))
                {
                    loginHis.HavePlan_Medical__c = 1;
                    loginHis.Date__c = date.today() - 1;
                    SetRecord = True;                
                }
                if(RPMap.containsKey(u.id))
                {
                    loginHis.HavePlan_Trade__c = 1;
                    loginHis.Date__c = date.today() - 1;
                    SetRecord = True;                  
                }
                
                if(AllLogHistoryMap.containsKey(u.id))
                {
                    loginHis.Login_Date__c = date.today() - 1;
                    loginHis.Date__c = date.today() - 1;
                    SetRecord = True;                    
                }
                
                if(SetRecord == True){
                    insertList.add(loginHis);
                }
            }
            if(insertList.size() > 0)
            {
                insert insertList;
            }
        }
}